﻿using StoryTeller.Domain;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;
using StoryTeller.Infrastructure.DataAccess.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoryTeller.Infrastructure.DataAccess.DAL
{
    public class StoryDAL : BaseDAL, IStoryDAL
    {
        public void Insert(Story entidade)
        {
            Entities.Story.Add(entidade.ParseDomainToEntity());

            Save();
        }

        public void Update(Story entidade)
        {
            EStory usr = Entities.Story.First(c => c.Id == entidade.Id);

            usr.LastUpdate = DateTime.Now;

            Save();
        }

        public void Delete(int id)
        {
            EStory cat = Entities.Story.First(c => c.Id == id);

            if (cat != null)
            {
                Entities.Story.Remove(cat);

                Save();
            }
        }

        public ICollection<Story> Select()
        {
            ICollection<EStory> coll = Entities.Story.ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public Story Select(int id)
        {
            return Entities.Story.First(c => c.Id == id).ParseEntityToDomain();
        }

        public Pagination<Story> Select(Pagination<Story> pagination)
        {
            pagination.TotalItems = Entities.Story.Count();
            var query = from story in Entities.Story
                                .OrderBy(c => c.Id).Skip((pagination.PageNumber - 1) * pagination.PageSize).Take(pagination.PageSize)                 
                                select story;

            if (pagination.Filter != null)
            {
                var queryCount = from story in Entities.Story                            
                        where
                            (string.IsNullOrEmpty(pagination.Filter.Title) || story.Title == pagination.Filter.Title)
                            &&
                            (string.IsNullOrEmpty(pagination.Filter.Description) || story.Title == pagination.Filter.Description)
                            &&
                            (pagination.Filter.Category == ECategory.none || ((ECategory)story.CategoryId) == pagination.Filter.Category)
                        select story;

                pagination.TotalItems = queryCount.Count();
                query = from story in Entities.Story
                            .OrderBy(c => c.Id).Skip((pagination.PageNumber - 1) * pagination.PageSize).Take(pagination.PageSize)
                        where
                            (string.IsNullOrEmpty(pagination.Filter.Title) || story.Title == pagination.Filter.Title)
                            &&
                            (string.IsNullOrEmpty(pagination.Filter.Description) || story.Title == pagination.Filter.Description)
                            &&
                            (pagination.Filter.Category == ECategory.none || ((ECategory)story.CategoryId) == pagination.Filter.Category)
                        select story;
            }

            ICollection<EStory> coll = query.ToList();

            ChapterDAL dalChapter = new ChapterDAL();            

            pagination.List = coll.Select(c => c.ParseEntityToDomain()).ToList();

            pagination.List.ToList().ForEach(s => s.Chapters = dalChapter.SelectByStoryId(s.Id));

            return pagination;
        }
    }
}
